/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

define(['adminapp', 'spinner', 'bootstrap-daterangepicker'], function ($app) {
    $app.navactive('table_category');

    var num_tpl = Handlebars.compile($("#num_template").html());

    var category_brands_value = $("#category_brands_value").val();
    $(".spinner_el").spinner({value:0, min: 0});

    $app.formvalidator({
    }, 'dataForm');

    $("#category_brands").change(function () {
        var value = $(this).val() || "";
        value = value + "";
        if (category_brands_value != value) {
            var vals = [];
            var cVals = [];
            if (category_brands_value) {
                vals = category_brands_value.split(",");
            }
            if (value) {
                cVals = value.split(",");
            }

            if (category_brands_value) {
                if (vals.length > cVals.length) {
                    var cut = filter(vals, cVals);
                    var id = cut[0];
                    //$("#category_brands").find("option[value='"+ cut[0] +"']").html();
                    $("#num_area").find("#num_div_" + id).remove();
                } else if(vals.length < cVals.length) {
                    var add = filter(cVals, vals);
                    var id = add[0];
                    var name = $("#category_brands").find("option[value='"+ add[0] +"']").html();
                    var ctx = {categoryid : id, categoryname : name};
                    $("#num_area").append(num_tpl(ctx));

                    $("#spinner_el_num_" + id).spinner({value:0, min: 0});
                    $("#spinner_el_min_seats_" + id).spinner({value:0, min: 0});
                    $("#spinner_el_max_seats_" + id).spinner({value:0, min: 0});
                }
            } else {
                var id = value;
                var name = $("#category_brands").find("option[value='"+ id +"']").html();
                var ctx = {categoryid : id, categoryname : name};
                $("#num_area").append(num_tpl(ctx));

                $("#spinner_el_num_" + id).spinner({value:0, min: 0});
                $("#spinner_el_min_seats_" + id).spinner({value:0, min: 0});
                $("#spinner_el_max_seats_" + id).spinner({value:0, min: 0});
            }
            category_brands_value = value;
        }
    });

    function filter(big, small) {
        var arr = [];
        for (var i = 0, l = big.length; i < l; i++) {
            var b = big[i];
            if (!small.contains(b)) {
                arr[arr.length] = b;
            }
        }
        return arr;
    }

    Array.prototype.contains = function(obj) {
        var boo = false;
        for(var i=0;i<this.length;i++) {
            if(typeof obj == "object") {
                if(this[i].equals(obj)) {
                    boo = true;
                    break;
                }
            }else {
                if(this[i] == obj)  {
                    boo = true;
                    break;
                }
            }
        }
        return boo;
    }
});


